package com.wanban.downloader.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class M3U8DownloadSQLiteHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "wanban_m3u8_video_download.db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_VIDEO_DOWNLOAD_INFO = "m3u8_video_download";

    public static class Columns {
        public static final String URL = "url";

        public static final String M3U8 = "m3u8";
        public static final String STATE = "state";
        public static final String HEADER = "header";
        public static final String EPISODE = "episode";
        public static final String VIDEO_ID = "videoId";
        public static final String SOURCE_ID = "sourceId";
        public static final String COVER_URL = "coverUrl";
        public static final String VIDEO_NAME = "videoName";
        public static final String EPISODE_NAME = "episodeName";
        public static final String CONTENT_LENGTH = "contentLength";
        public static final String FILE_PATH = "filePath";
        public static final String COVER_FILE_PATH = "coverFilePath";
        public static final String RETRY_TIME = "retryTime";
        public static final String WATCH_TIME = "watchTime";

    }

    public M3U8DownloadSQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createVideoDownloadInfoTable(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        if (oldVersion == 1) {
//            upgradeDatabaseToVersion2(db);
//        }
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    private void createVideoDownloadInfoTable(SQLiteDatabase db) {
        // 删除旧表
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEO_DOWNLOAD_INFO);
        // 创建新表
        db.execSQL("CREATE TABLE " + TABLE_VIDEO_DOWNLOAD_INFO + "("
                + Columns.URL + " TEXT PRIMARY KEY, "
                + Columns.VIDEO_ID + " INTEGER, "
                + Columns.STATE + " INTEGER, "
                + Columns.EPISODE + " INTEGER, "
                + Columns.SOURCE_ID + " INTEGER, "
                + Columns.RETRY_TIME + " INTEGER, "
                + Columns.WATCH_TIME + " INTEGER, "
                + Columns.CONTENT_LENGTH + " BIGINT, "
                + Columns.COVER_URL + " TEXT, "
                + Columns.VIDEO_NAME + " TEXT, "
                + Columns.EPISODE_NAME + " TEXT, "
                + Columns.FILE_PATH + " TEXT, "
                + Columns.COVER_FILE_PATH + " TEXT, "
                + Columns.HEADER + " TEXT, "
                + Columns.M3U8 + " TEXT);");
    }

    private void upgradeDatabaseToVersion2(SQLiteDatabase db) {
        db.execSQL("ALTER TABLE " + TABLE_VIDEO_DOWNLOAD_INFO + " ADD COLUMN " + Columns.COVER_URL + " TEXT");
    }

}
